package avicit.ma.ma.na.demomain.rest;

import avicit.ma.ma.na.demomain.dto.*;
import avicit.ma.ma.na.demomain.dto.DemoMainDTO;
import avicit.ma.ma.na.demomain.service.DemoMainService;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.StringUtils;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.domain.ValidationList;
import avicit.platform6.core.excel.ExcelUtil;
import avicit.platform6.core.excel.imp.entity.ExcelImportResult;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.core.rest.msg.QueryReqBean;
import avicit.platform6.core.rest.msg.QueryReqBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;

/**
 * demoMainRest类
 *
 * @author admin
 * @version V1.0.0
 * @date 2024-11-18 14:17:22
 * @since V1.0.0
 */
@RestController
@Api(tags = "DemoMain", value = "demoMain")
@RequestMapping("api/ma/na/demomain")
public class DemoMainRest {

  private static final Logger logger = LoggerFactory.getLogger(DemoMainRest.class);

  @Autowired
  DemoMainService demoMainService;

  @PostMapping("/find-list-by-page/v1")
  @ApiOperation(value = "按条件分页查询主表数据")
  @RequiresPermissions("demoMain:view")
  public ResponseMsg<QueryRespBean<DemoMainDTO>> findListByPage(@ApiParam(value = "", name = "queryReqBean") @RequestBody QueryReqBean<DemoMainDTO> queryReqBean) {
    ResponseMsg<QueryRespBean<DemoMainDTO>> responseMsg = new ResponseMsg<>();
    QueryRespBean<DemoMainDTO> result = demoMainService.findListByPage(queryReqBean);
    responseMsg.setResponseBody(result);
    SysLogUtil.log("demo_main表", "查询demo_main表信息", PlatformConstant.OpType.select);
    return responseMsg;
  }

  @PostMapping("/find-list/v1")
  @ApiOperation(value = "按条件不分页查询主表数据")
  public ResponseMsg<List<DemoMainDTO>> findList(@ApiParam(value = "", name = "queryReqBean") @RequestBody QueryReqBean<DemoMainDTO> queryReqBean) {
    ResponseMsg<List<DemoMainDTO>> responseMsg = new ResponseMsg<>();
    List<DemoMainDTO> result = demoMainService.findList(queryReqBean);
    responseMsg.setResponseBody(result);
    SysLogUtil.log("demo_main表", "查询demo_main表信息", PlatformConstant.OpType.select);
    return responseMsg;
  }

  @GetMapping("/get/{id}/v1")
  @ApiOperation(value = "通过主键查询主表单条记录")
  @RequiresPermissions("demoMain:edit")
  public ResponseMsg<DemoMainDTO> getById(@ApiParam(value = "", name = "id") @PathVariable("id") String id) {
    ResponseMsg<DemoMainDTO> responseMsg = new ResponseMsg<>();
    DemoMainDTO demoMain = demoMainService.get(id);
    responseMsg.setResponseBody(demoMain);
    SysLogUtil.log4Query(demoMain);
    return responseMsg;
  }

  @PostMapping("/save/v1")
  @ApiOperation(value = "新增主表对象")
  @RequiresPermissions(value = { "demoMain:add", "demoMain:edit" }, logical = Logical.OR)
  public ResponseMsg<String> save(@ApiParam(value = "", name = "demoMain") @RequestBody DemoMainDTO demoMain) {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    if (StringUtils.isEmpty(demoMain.getId())) {
      responseMsg.setResponseBody(demoMainService.insert(demoMain));
    } else {
      responseMsg.setResponseBody(String.valueOf(demoMainService.updateAll(demoMain)));
    }
    return responseMsg;
  }

  @PutMapping("/update-sensitive/v1")
  @ApiOperation(value = "修改主表部分对象字段")
  public ResponseMsg<Integer> updateSensitive(@ApiParam(value = "", name = "demoMain") @RequestBody DemoMainDTO demoMain) {
    ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(demoMainService.updateSensitive(demoMain));
    return responseMsg;
  }

  @PutMapping("/update-all/v1")
  @ApiOperation(value = "修改主表全部对象字段")
  public ResponseMsg<Integer> updateAll(@ApiParam(value = "", name = "demoMain") @RequestBody DemoMainDTO demoMain) {
    ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(demoMainService.updateAll(demoMain));
    return responseMsg;
  }

  @DeleteMapping("/delete-by-id/{id}/v1")
  @ApiOperation(value = "按主键单条删除")
  public ResponseMsg<Integer> deleteById(@ApiParam(value = "", name = "id") @PathVariable("id") String id) {
    ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(demoMainService.delete(id));
    return responseMsg;
  }

  @DeleteMapping("/delete-by-ids/v1")
  @ApiOperation(value = "主表数据批量删除")
  @RequiresPermissions("demoMain:delete")
  public ResponseMsg<Integer> deleteByIds(@ApiParam(value = "", name = "ids") @RequestBody String[] ids) {
    ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(demoMainService.deleteByIds(ids));
    return responseMsg;
  }

  @GetMapping("/download-template/v1")
  @ApiOperation(value = "模板下载")
  public void downloadTemplate(HttpServletResponse response) throws IOException {
    ExcelUtil.downloadTemplate(response, DemoMainDTO.class, new DemoMainDTO().getLogTitle());
  }

  @PostMapping("/import-data/v1")
  @ApiOperation(value = "导入文件")
  @RequiresPermissions("demoMain:import")
  public ResponseMsg<Object> importDataV2(@ApiParam(value = "", name = "impExcelFile") @RequestPart("impExcelFile") MultipartFile impExcelFile, @ApiParam(value = "", name = "formData") @RequestParam("formData") String formData) throws IOException {
    ResponseMsg<Object> responseMsg = new ResponseMsg<>();
    List<Map<String, String>> excelList = ExcelUtil.importExcel(multipartFile.getInputStream(), DemoMainImportDTO.class);
    ExcelImportResult excelImportResult = demoMainService.importData(excelList, formData);
    responseMsg.setResponseBody(excelImportResult);
    return responseMsg;
  }

  @PostMapping("/export-data/v1")
  @ApiOperation(value = "按条件导出所有")
  @RequiresPermissions("demoMain:export")
  public void exportData(@ApiParam(value = "", name = "demoMain") @RequestBody QueryReqBean<DemoMainDTO> demoMain, HttpServletResponse response) throws IOException {
    List<DemoMainDTO> list = demoMainService.findList(demoMain);
    ExcelUtil.exportExcel(response, list, DemoMainDTO.class, "导出数据", "sheet1");
    SysLogUtil.log(new DemoMainDTO().getLogTitle(), new DemoMainDTO().getLogTitle() + "共导出" + list.size() + "条数据", PlatformConstant.OpType.fileExport, PlatformConstant.OpResult.success, ComUtil.getId(), "demo_main");
  }
}
